commit e9ccb1d8d095482e4139da658e679d5aad56b184
Author: Tim-Philipp Müller <tim@centricular.com>
Date:   Tue Dec 15 00:12:55 2020 +0000

    meson: fix library versioning
    
    Fixes #442

diff --git a/meson.build b/meson.build
index b159b4071..53a9af343 100644
--- a/meson.build
+++ b/meson.build
@@ -3,8 +3,6 @@ project('cairo', 'c', 'cpp',
   version: run_command(find_program('version.py'), check: true).stdout().strip(),
 )
 
-cc = meson.get_compiler('c')
-
 # Keep in sync with configure.ac!
 freetype_required_version = '>= 9.7.3'
 fontconfig_required_version = '>= 2.2.95'
@@ -16,6 +14,27 @@ libudev_required_version = '>= 136'
 libdrm_required_version = '>= 2.4'
 glib_required_version = '>= 2.14'
 
+# library versioning
+version_arr = meson.project_version().split('.')
+cairo_version_major = version_arr[0].to_int()
+cairo_version_minor = version_arr[1].to_int()
+cairo_version_micro = version_arr[2].to_int()
+
+# The libtool shared library version stuff.
+# Try and maintain compatibility with the previous library versioning.
+cairo_version_sonum = cairo_version_major + 1
+cairo_version =  cairo_version_major * 10000 + cairo_version_minor * 100 + cairo_version_micro
+
+if cairo_version_minor % 2 == 1
+  # unstable release
+  cairo_libversion = '@0@.@1@.0'.format(cairo_version_sonum, cairo_version)
+else
+  # stable release
+  cairo_libversion = '@0@.@1@.@2@'.format(cairo_version_sonum, cairo_version, cairo_version_micro)
+endif
+
+cc = meson.get_compiler('c')
+
 if cc.get_id() == 'msvc'
   # Basic usage in the cairo type system that causes spammy and useless warnings
   add_project_arguments('/wd4244', '/wd4146',
diff --git a/src/meson.build b/src/meson.build
index 5bd1a57ee..f5d741eae 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -299,8 +299,8 @@ libcairo = library('cairo', cairo_sources,
   c_args: cairo_no_warn_c_args + pthread_c_args + ['-DHAVE_CONFIG_H'],
   cpp_args: cairo_no_warn_c_args + pthread_c_args + ['-DHAVE_CONFIG_H'],
   link_args: extra_link_args,
-  soversion: '2',
-  version: '2.' + ''.join(meson.project_version().split('.')) + '.0',
+  soversion: cairo_version_sonum,
+  version: cairo_libversion,
   install: true,
   include_directories: incbase,
 )
diff --git a/util/cairo-gobject/meson.build b/util/cairo-gobject/meson.build
index 200fd8705..bf24242a0 100644
--- a/util/cairo-gobject/meson.build
+++ b/util/cairo-gobject/meson.build
@@ -12,8 +12,8 @@ libcairogobject = library('cairo-gobject', cairo_gobject_sources,
   include_directories: [incbase, incsrc],
   dependencies: [glib_dep, gobject_dep],
   link_with: [libcairo],
-  soversion: '2',
-  version: '2.' + ''.join(meson.project_version().split('.')) + '.0',
+  soversion: cairo_version_sonum,
+  version: cairo_libversion,
   install: true,
 )
 
diff --git a/util/cairo-script/meson.build b/util/cairo-script/meson.build
index a782ec63f..8cedc6834 100644
--- a/util/cairo-script/meson.build
+++ b/util/cairo-script/meson.build
@@ -30,8 +30,8 @@ libcairoscript = library('cairo-script-interpreter',
   include_directories: [incbase, incsrc],
   dependencies: deps,
   link_with: [libcairo],
-  soversion: '2',
-  version: '2.' + ''.join(meson.project_version().split('.')) + '.0',
+  soversion: cairo_version_sonum,
+  version: cairo_libversion,
   install: true,
 )
 
